$(document)
		.ready(
				function() {
					$("#addissuebtnsave")
							.click(
									function() {

										var htmltext = CKEDITOR.instances.issuedetails
												.getData();
										// alert("Content: "+ htmltext);
										alert("CAtegpru: "
												+ $("#category option:selected")
														.text());
										$
												.ajax({
													type : "post",
													url : "../addissue?action=add",
													datatype : "json",
													data : "issuetitle="
															+ $("#issuetitle")
																	.val()
															+ "&bugid="
															+ $("#bugid").val()
															+ "&version="
															+ $("#version")
																	.chosen()
																	.val()
															+"&user="
															+user
															+ "&moduleid="
															+ $("#module")
																	.chosen()
																	.val()
															+ "&module="
															+ $(
																	"#module option:selected")
																	.text()
															+ "&category="
															+ $(
																	"#category option:selected")
																	.text()
															+ "&subcategory="
															+ $(
																	"#subcategory option:selected")
																	.text()
															+ "&categoryid="
															+ $("#category")
																	.chosen()
																	.val()
															+ "&subcategoryid="
															+ $("#subcategory")
																	.chosen()
																	.val()
															+ "&issuedetails="
															+ escape(htmltext),
													success : function(data) {
														if (data.issueinfo.length > 0) {
															var issueid = data.issueinfo[0].issueid
															alert("Issueid: "
																	+ issueid);
															window.location = "viewissue.jsp?issueid="
																	+ issueid;
														} else {
															Alert("Error saving issue");
														}
													},
													error : function(jqXHR,
															textStatus) {
														alert("Request failed: "
																+ textStatus);
													}
												});
									});

					$.getJSON('../GetCategories?parentid=0', function(data) {
						var i = 0;
						$("#module").html("<option> </option>");
						for (i = 0; i < data.modules.length; i++) {
							$("#module").append(
									'<option value="' + data.modules[i].id
											+ '">'
											+ data.modules[i].sectionname
											+ '</option>');
						}
						$("#module").trigger("liszt:updated");
					});
					

					$.getJSON('../GetVersions', function(data) {
						var i = 0;
						$("#version").html("<option> </option>");
						for (i = 0; i < data.versions.length; i++) {
							$("#version").append(
									'<option value="' + data.versions[i] + '">'
											+ data.versions[i] + '</option>');
							// alert("Version: "+data.versions[i] );
						}
						$("#version").trigger("liszt:updated");
					});

					$("#module")
							.change(
									function() {
										// alert("You changed value to: " +
										// $("#module option:selected").text() +
										// " moduleid: " +
										// this.value.toString());
										var parentcategory;
										parentcategory = this.value;
										modulename = $(
												"#module option:selected")
												.text();
										moduleid = this.value.toString();
										categoryname = "0";
										categoryid = "0";
										subcategoryid = "0";
										subcategoryid = "0";

										$
												.getJSON(
														'../GetCategories?parentid='
																+ parentcategory,
														function(data) {
															var i = 0;
															$("#category")
																	.html(
																			"<option> </option>");
															for (i = 0; i < data.modules.length; i++) {
																$("#category")
																		.append(
																				'<option value="'
																						+ data.modules[i].id
																						+ '">'
																						+ data.modules[i].sectionname
																						+ '</option>');
															}
															$('#category')
																	.trigger(
																			"liszt:updated");
														});

									});
					$("#category")
							.change(
									function() {
										// alert("You changed country to: " +
										// $("#module option:selected").text() +
										// " moduleid: " +
										// this.value.toString());
										var parentcategory;
										categoryname = $(
												"#category option:selected")
												.text();
										categoryid = this.value.toString();
										subcategoryid = "0";
										subcategoryid = "0";

										parentcategory = this.value;
										$
												.getJSON(
														'../GetCategories?parentid='
																+ parentcategory,
														function(data) {
															var i = 0;
															$("#subcategory")
																	.html(
																			"<option> </option>");
															for (i = 0; i < data.modules.length; i++) {
																$(
																		"#subcategory")
																		.append(
																				'<option value="'
																						+ data.modules[i].id
																						+ '">'
																						+ data.modules[i].sectionname
																						+ '</option>');
															}
															$('#subcategory')
																	.trigger(
																			"liszt:updated");
														});

									});

					$("#subcategory").change(
							function() {
								subcategoryname = $(
										"#subcategory option:selected").text();
								subcategoryid = this.value.toString();
							});

				});

function ViewPageInit(issueid, role) {

	$
			.ajax({
				type : "post",
				url : "../GetIssueDetail",
				datatype : "json",
				data : "issueid=" + issueid,
				success : function(data) {
					if (data.issuedetails.length > 0) {
						// alert("issuedetails: " +
						// data.issuedetails[0].issuedetails);
						$("#issuetitle").html(data.issuedetails[0].issuetitle);
						$("#issuedetail").html(
								data.issuedetails[0].issuedetails);
						if (data.issuedetails[0].module.length > 0) {
							$("#module").html(data.issuedetails[0].module);
						} else {
							$("#module").html("N.A.");
						}
						if (data.issuedetails[0].category.length > 0) {
							$("#module").append("  -->  ");
							$("#module").append(data.issuedetails[0].category);
						}
						if (data.issuedetails[0].subcategory.length > 0) {
							$("#module").append("  -->  ");
							$("#module").append(
									data.issuedetails[0].subcategory);
						}

						if (data.issuedetails[0].version.length > 0) {
							$("#version").html(data.issuedetails[0].version);
						} else {
							$("#version").html("N.A.");
						}
						if (data.issuedetails[0].bugid.length > 0) {
							$("#bugid").html(data.issuedetails[0].bugid);
						} else {
							$("#bugid").html("N.A.");
						}

						if (data.issuedetails[0].comments.length > 0) {
							var j;
							$("#readcomments").html("<div>");
							var text;
							for (j = 0; j < data.issuedetails[0].comments.length; j++) {

								text = "<div class=\" viewmycomment \" >"
										+ data.issuedetails[0].comments[j].comment
										+ "&nbsp;&nbsp;&nbsp;";

								if (role == '2') {
									text = text
											+ "<a href=\"javascript:void(0)\" style=\" cursor:pointer \"  onclick=\"editcomment('"
											+ data.issuedetails[0].comments[j].id
											+ "')\"><img src=\"../images/Edit.png\"  style=\"display: inline; \" width=\"16\" height=\"16\"/></a>"

											+ "&nbsp;&nbsp;&nbsp;"

											+ "<a  href=\"javascript:void(0)\" style=\"cursor:pointer\"  onclick=\"deletecomment('"
											+ data.issuedetails[0].comments[j].id
											+ "')\" > <img src=\"../images/SettingsDelete.png\" style=\"display: inline; \" width=\"16\" height=\"16\"/></a>";
								}

								text=text+"<br><b>  by  </b>"
										+ "<a href=\"javascript:void(0)\"><span> "
										+ data.issuedetails[0].comments[j].username
										+ "</span></a><b>  on  </b>"
										+ data.issuedetails[0].comments[j].date
										+ "<br/></div>";

								$("#readcomments").append(text);
								$("#readcomments").append("<br/><div>");

							}
						}

					}

				},
				error : function(jqXHR, textStatus) {
					alert("Error viewing article: " + textStatus);
				}
			});

};

$(document).ready(function() {

	$("#jstree").jstree({
		json_data : {
			ajax : {
				url : "../GetTreeData",
				type : "POST",
				dataType : "json",
				contentType : "application/json charset=utf-8",
				success : function(data) {

				}
			}
		},
		"themes" : {
			"theme" : "mytree",
			"dots" : false,
			"icons" : true
		// "url": "/css/jstree/themes/default/style.css"
		},

		"plugins" : [ "themes", "json_data", "ui" ]

	}).bind("select_node.jstree", function(e, data) {
		// alert(data.rslt.obj.data("id"));
		var src = data.rslt.obj.data("id");
		$("#usertable").jqGrid('setGridParam', {
			url : "../GetIssueList?folderid=" + src,
			page : 1
		}).trigger("reloadGrid");

	});

	$("#search").click(function() {

		var searchfield = jQuery("#corpusername").val();
		$("#usertable").jqGrid('setGridParam', {
			url : "../GetUserRoles?searchfield=" + searchfield,
			page : 1
		}).trigger("reloadGrid");
	});

	$("#usertable").jqGrid({
		url : '../GetIssueList',
		datatype : 'json',
		mtype : 'post',
		colNames : [ 'ID', 'title', 'Version', 'Bugid' ],
		colModel : [

		{
			name : 'id',
			index : 'id',
			width : 20,
			align : 'center',
			sortable : false,
			resizable : false,
			hidden : true
		}, {
			name : 'title',
			index : 'title',
			width : 180,
			// hidden : true,
			align : 'left',
			sortable : true,
			resizable : true,
			cellattr : function(rowId, tv, rawObject, cm, rdata) {
				return 'style="white-space: normal;"';
			}
		}, {

			name : 'version',
			index : 'version',
			width : 80,
			align : 'left',
			sortable : true,
			resizable : true,
			cellattr : function(rowId, tv, rawObject, cm, rdata) {
				return 'style="white-space: normal;"';
			}

		}, {

			name : 'bugid',
			index : 'bugid',
			width : 80,
			align : 'left',
			sortable : true,
			resizable : true,
			cellattr : function(rowId, tv, rawObject, cm, rdata) {
				return 'style="white-space: normal;"';
			}

		} ],
		pager : $('#pager'),
		altRows : false,
		width : $(window).width() - 300,
		height : $(window).height() - 300,
		rowNum : 10,
		rowList : [ 10, 20, 30 ],
		viewrecords : true,
		caption : "List of Known Issues",
		rownumbers : true,
		multiselect : false,
		multiboxonly : false,
		loadui : "enable",
		loadtext : "Loading issue list ...",
		recordtext : "Viewing {0} - {1} of {2}",
		emptyrecords : "Check connectivity to Database!",
		editurl : "../saverole",

		onSelectRow : function(id) {
			// alert("ID: "+ id );
		},
		ondblClickRow : function(id) {
			window.location = "viewissue.jsp?issueid=" + id;

		}
	});

	$("#usertable").jqGrid('navGrid', "#pager", {
		edit : false,
		add : false,
		del : false,
		search : false
	});
	$("#usertable").jqGrid('inlineNav', "#pager", {
		edit : true,
		add : false,
		del : false,
		search : false
	});

});
